//
//  HBScrollEmptyView.m
//  HBLibary
//
//  Created by Rambo on 2018/6/6.
//

#import "HBScrollEmptyView.h"
#import "UIView+HBFrame.h"

@interface HBScrollEmptyView (){
    CGFloat _contentWith;
    CGFloat _contentHeight;
    BOOL _isSetupContentViewY;
}

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *detailLabel;

@property (nonatomic, strong) UIButton *button;

@end

@implementation HBScrollEmptyView

- (void)prepare{
    [super prepare];
    _subViewMargin = 20.0f;
    _contentHorizontalMargin = 30.f;
    _contentVerticalMargin = 30.f;
    _titleFont = [UIFont systemFontOfSize:16];
    _titleColor = [UIColor blackColor];
    _detailFont = [UIFont systemFontOfSize:16];
    _detailColor = [UIColor darkGrayColor];
    _buttonTitleHorizontalMargin = 15;
    _buttonTitltFont = [UIFont systemFontOfSize:16];
    _detailTextAlignment = NSTextAlignmentLeft;
    _imageSize = CGSizeZero;
    _buttonLayerBorderColor = [UIColor lightGrayColor];
    _buttonLayerBorderWidth = 1;
    _buttonLayerCornerRadius = 3;
    _buttonBackgroundColor = [UIColor whiteColor];
    _buttonTitleColor = [UIColor blackColor];
    _buttonTitleVerticalMargin = 4;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    UIView *superView = self.superview;
    if (!superView) return;
    
    [superView layoutIfNeeded];
    
    [self emptyBaseView_layoutSubviews];
    
}

- (void)emptyBaseView_layoutSubviews{
    //    NSLog(@"%s",__func__);
    
    [super emptyBaseView_layoutSubviews];
    
    UIView *superView = self.superview;
    if (!superView) return;
    
    // remove contentView subview
    if (_imageView && !self.image) {
        [_imageView removeFromSuperview];
    }
    if (_titleLabel && (!self.title || self.title.length <= 0)) {
        [_titleLabel removeFromSuperview];
    }
    if (_detailLabel && (!self.detail || self.detail.length <= 0)) {
        [_detailLabel removeFromSuperview];
    }
    if (_button && (!self.buttonTitle || self.buttonTitle.length <= 0)) {
        [_button removeFromSuperview];
    }
    
    // draw contentView
    if (self.customView) {
        
        // add custonView to contentView
        [self.contentView addSubview:self.customView];
        _contentWith = self.customView.hb_width;
        _contentHeight = self.customView.hb_height;
        
    }else{
        // draw contentView subview
        [self drawContentSubview];
    }
    
    // self size
    if (self.isEqualSuperViewFrame) {
        self.hb_size = superView.hb_size;
    }else{
        self.hb_size = CGSizeMake(_contentWith, _contentHeight);
    }
    
    // self point
    self.hb_centerX = superView.hb_centerX;
    if (_isSetupContentViewY) {
        self.hb_y = self.contentViewY;
    }else{
        self.hb_centerY = superView.hb_centerY;
    }
    
    // contentView frame
    self.contentView.hb_size = CGSizeMake(_contentWith, _contentHeight);
    self.contentView.hb_centerX = self.hb_width * 0.5;
    self.contentView.hb_centerY = self.hb_height * 0.5;
    
    // contentView subview point
    if (_imageView)  self.imageView.hb_centerX = self.contentView.hb_width * 0.5;
    if (_titleLabel)    self.titleLabel.hb_centerX = self.contentView.hb_width * 0.5;
    if (_detailLabel)  self.detailLabel.hb_centerX = self.contentView.hb_width * 0.5;
    if (_button)        self.button.hb_centerX = self.contentView.hb_width * 0.5;
}

- (void)drawContentSubview{
    UIView *superView = self.superview;
    self.hb_width = superView.hb_width - 2*self.contentHorizontalMargin;
    self.hb_height = superView.hb_height - 2*self.contentVerticalMargin;
    
    _contentWith = 0;
    _contentHeight = 0;
    
    CGFloat y = 0;
    
    // draw imageView
    if (self.image) {
        [self.contentView addSubview:self.imageView];
        self.imageView.image = self.image;
        CGSize imageSize = self.image.size;
        if (!CGSizeEqualToSize(self.imageSize, CGSizeZero)) {
            imageSize = self.imageSize;
            if (imageSize.width > self.hb_width) {
                imageSize.width = self.hb_width;
            }
            if (imageSize.height > self.hb_height) {
                imageSize.height = self.hb_height;
            }
        }
        _contentWith = imageSize.width;
        _contentHeight = imageSize.height;
        self.imageView.frame = CGRectMake(0, y, imageSize.width, imageSize.height);
        y = self.imageView.hb_maxY + self.subViewMargin;
    }
    
    
    CGFloat contentMaxWidth = self.hb_width;
    CGFloat contentMaxHeight = self.hb_height;
    
    // draw titleLabel
    if (self.title && self.title.length > 0) {
        [self.contentView addSubview:self.titleLabel];
        self.titleLabel.text = self.title;
        CGSize titleLabelSize = [self textSizeWithText:self.title maxSize:CGSizeMake(contentMaxWidth, contentMaxHeight) font:self.titleFont];
        
        self.titleLabel.frame = CGRectMake(0, y, titleLabelSize.width, titleLabelSize.height);
        y = self.titleLabel.hb_maxY + self.subViewMargin;
        
        _contentWith = titleLabelSize.width > _contentWith ? titleLabelSize.width : _contentWith;
        
        _contentHeight = self.titleLabel.hb_maxY;
    }
    
    // draw detailLabel
    if (self.detail && self.detail.length > 0) {
        [self.contentView addSubview:self.detailLabel];
        self.detailLabel.text = self.detail;
        CGSize detailSize = [self textSizeWithText:self.detail maxSize:CGSizeMake(contentMaxWidth, contentMaxHeight) font:self.detailFont];
        
        self.detailLabel.frame = CGRectMake(0, y, detailSize.width, detailSize.height);
        
        y = self.detailLabel.hb_maxY + self.subViewMargin;
        
        _contentWith = detailSize.width > _contentWith ? detailSize.width : _contentWith;
        
        _contentHeight = self.detailLabel.hb_maxY;
        
    }
    
    // draw button
    if (self.buttonTitle && self.buttonTitle.length > 0) {
        [self.contentView addSubview:self.button];
        [self.button setTitle:self.buttonTitle forState:UIControlStateNormal];
        
        
        if (self.buttonTarget && self.buttonAction && [self.buttonTarget respondsToSelector:self.buttonAction]) {
            [self.button addTarget:self.buttonTarget action:self.buttonAction forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (self.buttonActionBlock) {
            [self.button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
        }
        
        CGSize buttonTitleSize = [self textSizeWithText:self.buttonTitle maxSize:CGSizeMake(contentMaxWidth, contentMaxHeight) font:self.buttonTitltFont];
        
        CGFloat buttonWidth = buttonTitleSize.width + 2*self.buttonTitleHorizontalMargin;
        CGFloat buttonHeight = buttonTitleSize.height + 2*self.buttonTitleVerticalMargin;
        
        self.button.frame = CGRectMake(0, y, buttonWidth, buttonHeight);
        
        _contentWith = buttonWidth > _contentWith ? buttonWidth : _contentWith;
        
        _contentHeight = self.button.hb_maxY;
    }
    
    if (self.viewTapBlock ) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapAction)];
        [self.contentView addGestureRecognizer:tap];
    }
    
    if (self.viewTarget && self.viewAction && [self.viewTarget respondsToSelector:self.viewAction]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.viewTarget action:self.viewAction];
        [self.contentView addGestureRecognizer:tap];
    }
}

#pragma mark - ---------------------Action-----------------------


- (void)viewTapAction{
    if (self.viewTapBlock) {
        self.viewTapBlock();
    }
}

- (void)buttonClick{
    if (self.buttonActionBlock) {
        self.buttonActionBlock();
    }
}

#pragma mark - ---------------------private method-----------------------

- (CGSize)textSizeWithText:(NSString*)text maxSize:(CGSize)maxSize font:(UIFont*)font{
    if (!text || text.length <= 0 || font == nil) {
        return CGSizeZero;
    }
    
    CGSize size = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
    
    return size;
    
}

#pragma mark - ----------------------set---------------------------

- (void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        self.titleLabel.font = _titleFont;
        [self layoutIfNeeded];
    }
}

- (void)setSubViewMargin:(CGFloat)subViewMargin{
    _subViewMargin = subViewMargin;
    
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setContentHorizontalMargin:(CGFloat)contentHorizontalMargin{
    _contentHorizontalMargin = contentHorizontalMargin;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setContentVerticalMargin:(CGFloat)contentVerticalMargin{
    _contentVerticalMargin = contentVerticalMargin;
    _isSetupContentViewY = NO;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    if (_titleLabel) {
        self.titleLabel.textColor = _titleColor;
    }
}

- (void)setDetailFont:(UIFont *)detailFont{
    _detailFont = detailFont;
    
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        self.detailLabel.font = _detailFont;
        [self layoutIfNeeded];
    }
}

- (void)setDetailColor:(UIColor *)detailColor{
    _detailColor = detailColor;
    if (_detailLabel) {
        self.detailLabel.textColor = _detailColor;
    }
}

- (void)setButtonTitleHorizontalMargin:(CGFloat)buttonTitleHorizontalMargin{
    _buttonTitleHorizontalMargin = buttonTitleHorizontalMargin;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setButtonTitltFont:(UIFont *)buttonTitltFont{
    _buttonTitltFont = buttonTitltFont;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        self.button.titleLabel.font = _buttonTitltFont;
        [self layoutIfNeeded];
    }
}

- (void)setButtonTitleVerticalMargin:(CGFloat)buttonTitleVerticalMargin{
    _buttonTitleVerticalMargin = buttonTitleVerticalMargin;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}


- (void)setButtonLayerBorderColor:(UIColor *)buttonLayerBorderColor{
    _buttonLayerBorderColor = buttonLayerBorderColor;
    if (!_buttonLayerBorderColor) {
        _buttonLayerBorderColor = [UIColor clearColor];
    }
    if (_button) {
        self.button.layer.borderColor = _buttonLayerBorderColor.CGColor;
    }
}

- (void)setButtonLayerBorderWidth:(CGFloat)buttonLayerBorderWidth{
    _buttonLayerBorderWidth = buttonLayerBorderWidth;
    if (_button) {
        self.button.layer.borderWidth = _buttonLayerBorderWidth;
    }
}

- (void)setButtonLayerCornerRadius:(CGFloat)buttonLayerCornerRadius{
    _buttonLayerCornerRadius = buttonLayerCornerRadius;
    if (_button) {
        self.button.layer.cornerRadius = _buttonLayerCornerRadius;
    }
}

- (void)setDetailTextAlignment:(NSTextAlignment)detailTextAlignment{
    _detailTextAlignment = detailTextAlignment;
    if (_detailLabel) {
        _detailLabel.textAlignment = _detailTextAlignment;
    }
}

- (void)setImageSize:(CGSize)imageSize{
    _imageSize = imageSize;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setButtonBackgrondColor:(UIColor *)buttonBackgrondColor{
    _buttonBackgroundColor = buttonBackgrondColor;
    if (_button) {
        self.button.backgroundColor = _buttonBackgroundColor;
    }
}

- (void)setButtonTitleColor:(UIColor *)buttonTitleColor{
    _buttonTitleColor = buttonTitleColor;
    if (_button) {
        [self.button setTitleColor:_buttonTitleColor forState:UIControlStateNormal];
    }
}

- (void)setContentViewY:(CGFloat)contentViewY{
    _contentViewY = contentViewY;
    _isSetupContentViewY = YES;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

- (void)setIsEqualSuperViewFrame:(BOOL)isEqualSuperViewFrame{
    _isEqualSuperViewFrame = isEqualSuperViewFrame;
    if (_titleLabel || _detailLabel || _imageView || _button || self.customView) {
        [self layoutIfNeeded];
    }
}

#pragma mark - ----------------------懒加载-------------------------

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = _titleColor;
        _titleLabel.font = _titleFont;
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = _detailFont;
        _detailLabel.textColor = _detailColor;
        _detailLabel.numberOfLines = 0;
        _detailLabel.textAlignment = _detailTextAlignment;
    }
    return _detailLabel;
}

- (UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.titleLabel.font = _buttonTitltFont;
        _button.layer.masksToBounds = YES;
        _button.layer.borderColor = _buttonLayerBorderColor.CGColor;
        _button.layer.borderWidth = _buttonLayerBorderWidth;
        _button.layer.cornerRadius = _buttonLayerCornerRadius;
        _button.backgroundColor = _buttonBackgroundColor;
        [_button setTitleColor:_buttonTitleColor forState:UIControlStateNormal];
    }
    return _button;
}

@end
